package opens.components.view;

import opens.components.cache.Cache;
import opens.components.http.ImageRequest;
import opens.components.http.core.HttpRequest;
import opens.components.http.core.RequestQueue;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.animation.Animation;
import android.widget.ImageView;

/**
 * Use the loadFromUrl method to asynchronously load the image from URL.
 * 
 * When the image is loaded, it can be animated into the view by setting the
 * appearAnimation.
 * 
 * @author Vatroslav Dino Matijas
 * 
 */
public class RemoteImageView extends ImageView {

	private Animation appearAnimation;
	private HttpRequest request;
	private RemoteImageViewListener listener;

	public RemoteImageView(Context context, AttributeSet attributeSet) {
		super(context, attributeSet);
	}

	public RemoteImageView(Context context) {
		super(context);
	}

	public void setRemoteImageViewListener(RemoteImageViewListener listener) {
		this.listener = listener;
	}

	public void setAppearAnimation(Animation appearAnimation) {
		this.appearAnimation = appearAnimation;
	}

	public void onImageRequestSuccess(ImageRequest request) {
		Bitmap bitmap = request.getResponseObject();
		setImageBitmap(bitmap);
		if (appearAnimation != null) {
			startAnimation(appearAnimation);
		}
		this.request = null;

		// notify listener
		if (null != this.listener) {
			listener.onImageRequestComplete(bitmap);
		}
	}

	public void onTimelineRequestError(ImageRequest request) {
		this.request = null;

		// notify listener
		if (null != this.listener) {
			listener.onTimelineRequestError(request);
		}
	}

	// TODO::gif supported
	public void loadFromUrl(String url, Cache cache) {
		ImageRequest imageRequest = new ImageRequest();
		imageRequest.onSuccess(this, "onImageRequestSuccess");
		imageRequest.onError(this, "onTimelineRequestError");
		imageRequest.setCache(cache);
		imageRequest
				.setCachePolicy(HttpRequest.FallbackToCacheIfLoadFailsCachePolicy);
		this.request = imageRequest;
		RequestQueue.instance().push(request.get(url));
	}

}
